﻿using Microsoft.VisualStudio.TeamFoundation;
using Microsoft.VisualStudio.TeamFoundation.VersionControl;
using Microsoft.VisualStudio.TeamFoundation.WorkItemTracking;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.VersionControl.Client;
using Microsoft.TeamFoundation.WorkItemTracking.Client;
using Microsoft.VisualStudio.Shell;
using System;

namespace W4S.W4SWorkItemTrackingPackage
{

    static class VSEnvironment
    {
        /// <summary>
        /// The top-level object in the Visual Studio automation object model.
        /// </summary>
        public static EnvDTE80.DTE2 CurrentDTE
        {
            get { return dte; }
            set { dte = value; }
        }

        public static TeamFoundationServerExt TeamFoundationServerExt
        {
            get { return dte.DTE.GetObject("Microsoft.VisualStudio.TeamFoundation.TeamFoundationServerExt") as TeamFoundationServerExt; }
        }

        public static ProjectContextExt ProjectContextExt
        {
            get { return TeamFoundationServerExt.ActiveProjectContext; }
        }

        /// <summary>
        /// Represents a connection to the server that is running Team Foundation Server.
        /// </summary>
        public static TfsConfigurationServer TfsConfigurationServer
        {
            get { return TfsConfigurationServerFactory.GetConfigurationServer(new Uri(ProjectContextExt.DomainUri)); }
        }

        /// <summary>
        /// Abstracts common infrastructure for the Team Foundation system and is the parent class for Team Foundation components.
        /// </summary>
        public static TfsTeamProjectCollection ProjectCollection
        {
            get { return TfsTeamProjectCollectionFactory.GetTeamProjectCollection(TfsConfigurationServer.Uri); }
        }

        public static VersionControlExt VersionControlExt
        {
            get { return dte.DTE.GetObject("Microsoft.VisualStudio.TeamFoundation.VersionControl.VersionControlExt") as VersionControlExt; }
        }

        /// <summary>
        /// Represents the version control repository.
        /// </summary>
        public static VersionControlServer VersionControlServer
        {
            get { return ProjectCollection.GetService<VersionControlServer>(); }
        }

        public static VersionControlExplorerExt VersionControlExplorerExt
        {
            get { return VersionControlExt.Explorer as VersionControlExplorerExt; }
        }

        /// <summary>
        /// Encapsulates the data store that contains all work items on a particular Team Foundation server. 
        /// Each work item store is scoped at the server level.
        /// </summary>
        public static WorkItemStore WorkItemStore
        {
            get { return ProjectCollection.GetService<WorkItemStore>(); }
        }

        /// <summary>
        /// DocumentService is the public service for creating, getting, and showing Work Item Tracking VSIP documents.
        /// </summary>
        public static DocumentService DocumentService
        {
            get { return documentService; }
            set { documentService = value; }
        }

        public static DocumentService documentService;
        public static EnvDTE80.DTE2 dte;
    }
}
